<?php

namespace App\Http\Controllers\Home;

use App\Http\Model\User;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Mail;

class LoginController extends Controller{

    /**
     * 首页用户登录
     * @return $this|array
     */
    public function login(){
        if($input=Input::all()){
            if($userinfo=User::where(['username'=>$input['username'],'password'=>$input['password']])->
            orwhere(['email'=>$input['username'],'password'=>$input['password']])->first()){
                if($userinfo['status']==1){
                    if($input['remember']=='1'){
                        Cookie::queue('username',$input['username'],3600*24*7);
                        Cookie::queue('password',$input['password'],3600*24*7);
                    }
                    session()->put(['user'=>$userinfo]);
                    return ['status'=>1,'msg'=>'登陆成功'];
                }elseif($userinfo['status']==3){
                    return ['status'=>0,'msg'=>'账号未激活'];
                }else{
                    return ['status'=>0,'msg'=>'账号已停用'];
                }
            }else{
                return ['status'=>0,'msg'=>'用户名或者密码错误'];
            }
        }
        return view('home.login')->with('title','用户登录');
    }

    /**
     * 用户注册
     * @return $this|array
     */
    public function register(){
        if($input=Input::all()){
            $user=new User();
            $user->username=$input['username'];
            if(User::where('username',$input['username'])->first()){
                return ['status'=>0,'msg'=>'用户名已经存在'];
            }
            $user->password=$input['password'];
            $user->email=$input['email'];
            if($user->save()){
                $data = ['username'=>$input['username'], 'time'=>time(),'token'=>
                    md5("sdgfdsg3246t4erg*&%&^".$input['username']),'email'=>$input['email']];
                $flag = Mail::send('emails.activemail',$data,function($message) use($data){
                    $message ->to($data['email'],$data['username'])->subject('激活账号');
                });
                return $flag ? ['status'=>1,'msg'=>'发送邮件成功，请查收！'] : ['status'=>0,'msg'=>'发送邮件成功，请重试！'];
            }else{
                return ['status'=>0,'msg'=>'注册失败'];
            }
        }
        return view('home.register')->with('title','用户注册');
    }

    /**
     * 用户忘记密码
     * @return $this|array
     */
    public function forget_pass(){
        if($email=Input::all()){
            if($userinfo=User::where('email',$email['email'])->first()){
                //发送邮件\
                $data = ['token'=>md5("sdgfdsgfindt4erg*&%&^".$email['email']),'email'=>
                    $email['email'],'uname'=>$userinfo['username']];
                $flag = Mail::send('emails.forgetpass',$data,function($message) use($data){
                    $message ->to($data['email'],$data['uname'])->subject('找回密码');
                });
                return $flag ? ['status'=>1,'msg'=>'发送邮件成功，请查收！'] : ['status'=>0,'msg'=>'发送邮件成功，请重试！'];
            }else{
                return ['status'=>0,'msg'=>'邮箱不正确'];
            }
        }
        return view('home.forget_pass')->with('title','找回密码');
    }

    /**
     * 用户修改密码
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function chagePass(Request $request){
        $username=$request->get('u');
        $email=$request->get('e');
        $token=$request->get('t');
        if(md5("sdgfdsgfindt4erg*&%&^".$email)!=$token){
            echo "<h1 style='text-align: center;margin-top: 20%'>未知错误</h1>";
        }else{
            return view('home.chagepass',compact('username'));
        }
    }

    /**
     * 用户更新密码
     * @param Request $request
     * @return array
     */
    public function updatePass(Request $request){
        $password=$request->get('password');
        $username=$request->get('username');
        if(User::where('username',$username)->update(['password'=>$password])){
            return ['status'=>1,'msg'=>'密码重置成功'];
        }else{
            return ['status'=>0,'msg'=>'密码重置失败'];
        }
    }

    /**
     * 用户激活账号
     * @param Request $request
     */
    public function active(Request $request){
        $username=$request->get('u');
        $token=$request->get('ts');
        $time=$request->get('t');
        if($time+60*60>time()){
            if($token!=md5("sdgfdsg3246t4erg*&%&^".$username)){
                echo "<h1 style='text-align: center;margin-top: 20%'>未知原因出现错误</h1>";
            }else{
                if(User::where('username',$username)->update(['status'=>1])){
                    echo "<h1 style='text-align: center;margin-top: 20%;'>账号已经激活</h1>";
                }else{
                    echo "<h1 style='text-align: center;margin-top: 20%'>账号未激活，未知错误</h1>";
                }
            }
        }else{
            echo "<h1 style='text-align: center;margin-top: 20%'>链接已过时</h1>";
        }
    }
}
